﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SGArchiveSystem7x.Controllers.Archive
{
    public class ArchiveInitController : BaseController
    {
        /// <summary>
        /// 本控制器的名称，用于权限访问
        /// </summary>
        private string sControllerName = "ArchiveInit";
        /// <summary>
        /// 档案初始化
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(string name)
        {
            var n = this.VaildUser(sControllerName);
            if (n != null)
                return n;

            ///清除Page级的Session
            App_Code.CSession.Clear_PageSession(Session);

            ///获取档案大类
            var listType = SGArchiveSystem7xDB.Linq.T_ArchiveTypeDal.Get_ArchiveType();
            ///获取档案类型
            var listTree = SGArchiveSystem7xDB.Linq.T_ArchiveTreeDal.Get_ArchiveTree(-1, 1);

            var listArchiveTree = Models.CArchiveTreeModel.Create_ArchiveTree(listType, listTree);
            ViewBag.ArchiveTree = listArchiveTree;

            ////获取当前用户可操作的部门
            //var bm = SGArchiveSystem7x.App_Code.CSession.Get_Bm(Session);
            //string[] ShowDepartmentID = bm.Dep_Show.Split(',');   ///可以在当档案列表里看到的部门ID，在列表里能显示的，即查档时添加 gdbm in xxx 条件
            //string[] ViewDepartmentID = bm.Dep_View.Split(',');     ///可以对哪几个部门的档案的电子文件进行查看，点击电子文件时进行判断
            //string[] PrintDepartmentID = bm.Dep_Print.Split(',');     ///可以对哪几个部门的档案的电子文件下载，跳转到电子文件时进行判断
            //string[] DownDepartmentID = bm.Dep_Down.Split(',');         ///可以对哪几个部门的档案进行电子文件下载，跳转到电子文件时进行判断
            //string[] EditDepartmentID = bm.Dep_Edit.Split(',');       ///可以对哪几个部门的档案进行添加、编辑、上传电子文件，点击这些按钮时进行判断

            /////档案密级，每个档案都有密级，如果此密级大于等于档案的密级，那么可以根据部门权限进行操作，如果小于那么无论部门权限如何，都不进行操作
            //int Mj = SGArchiveSystem7x.App_Code.CSession.Get_User(Session).ROLE_MJ.Value;  
            ViewBag.Name = name;
            return View();
        }
        public ActionResult ArchiveAJ(string arid, string type, string sn)
        {
            if(Models.Language._nowLanguage == SGArchiveSystem7xDB._VersionLanguage._Chs)
            {
                ViewBag.Text1 = "编号:";
                ViewBag.Text2 = "批次号:";
                ViewBag.Text3 = "品名:";
                ViewBag.Text4 = "是否在库:";
                ViewBag.Text5 = "快速查询";
                ViewBag.Text6 = "组合查询";
                ViewBag.Text7 = "人工出库";
                ViewBag.Text8 = "人工入库";
                ViewBag.Text9 = "提示";
                ViewBag.Text10 = "请选择在库的物品!";
                ViewBag.Text11 = "请选择要登记出库的物品!";
                ViewBag.Text12 = "出库操作";
                ViewBag.Text13 = "存在已经出库的物品";
                ViewBag.Text14 = "请选择在库的货物进行人工出库操作!";
                ViewBag.Text15 = "请选择要人工出库的物品!";
                ViewBag.Text16 = "您已经有很长时间没有进行操作了，请重新登录系统!";
                ViewBag.Text17 = "操作失败!";
                ViewBag.Text18 = "请选择已出库的货物进行人工入库操作!";
                ViewBag.Text19 = "请选择要人工入库的物品!";
            }
            else
            {
                ViewBag.Text1 = "Number:";
                ViewBag.Text2 = "Batch Number:";
                ViewBag.Text3 = "Name:";
                ViewBag.Text4 = "Status:";
                ViewBag.Text5 = "Fast Search";
                ViewBag.Text6 = "Adv Search";
                ViewBag.Text7 = "Operation(OUT)";
                ViewBag.Text8 = "Operation(IN)";
                ViewBag.Text9 = "Tip";
                ViewBag.Text10 = "Please select items in the warehouse";
                ViewBag.Text11 = "Please select the items to be shipped out of the warehouse";
                ViewBag.Text12 = "Out Warehouse";
                ViewBag.Text13 = "There are items that have been shipped out of the warehouse";
                ViewBag.Text14 = "Please select the items in the warehouse for manual delivery";
                ViewBag.Text15 = "Please select the items for manual delivery";
                ViewBag.Text16 = "You have not been working for a long time, please log in again";
                ViewBag.Text17 = "operation failed";
                ViewBag.Text18 = "Please select the items that are not in the library for manual delivery";
                ViewBag.Text19 = "Please select the items for manual delivery";
            }

            ////获取当前用户可操作的部门
            int count;
            var bm = SGArchiveSystem7x.App_Code.CSession.Get_Bm(Session);
            string ShowDepartmentID = bm.Dep_Show;   ///可以在当档案列表里看到的部门ID，在列表里能显示的，即查档时添加 gdbm in xxx 条件aa = aa.Substring(0, aa .Length - 1)
            string ViewDepartmentID = bm.Dep_View;     ///可以对哪几个部门的档案的电子文件进行查看，点击电子文件时进行判断
            string PrintDepartmentID = bm.Dep_Print;     ///可以对哪几个部门的档案的电子文件下载，跳转到电子文件时进行判断
            string DownDepartmentID = bm.Dep_Down;         ///可以对哪几个部门的档案进行电子文件下载，跳转到电子文件时进行判断
            string EditDepartmentID = bm.Dep_Edit;       ///可以对哪几个部门的档案进行添加、编辑、上传电子文件，点击这些按钮时进行判断
            ///档案密级，每个档案都有密级，如果此密级大于等于档案的密级，那么可以根据部门权限进行操作，如果小于那么无论部门权限如何，都不进行操作//无添加，添加时档案还不存在密集
            int Mj = SGArchiveSystem7x.App_Code.CSession.Get_User(Session).ROLE_MJ.Value;//发布、修改、删除、浏览电子件
            List<string> list1 = new List<string>(2);
            if (ShowDepartmentID == null || ShowDepartmentID.Length == 0)
                list1.Add("GDBM in (0)");
            else
                list1.Add("GDBM in ("+ ShowDepartmentID.Substring(0, ShowDepartmentID.Length - 1) + ")");
            list1.Add("");
            App_Code.CSession.Add_QueryWhere(Session, list1);
            ViewBag.ViewDepartmentID = ViewDepartmentID;
            ViewBag.PrintDepartmentID = PrintDepartmentID;
            ViewBag.DownDepartmentID = DownDepartmentID;
            ViewBag.EditDepartmentID = EditDepartmentID;
            ViewBag.Mj = Mj;
            ViewBag.TableMJ= SGArchiveSystem7xDB.Linq.T_ArchiveWordDal.Get_Word(null);
            ViewBag.Dep = SGArchiveSystem7xDB.Linq.T_DeparetmentDal.Get_Department(out count);
            ViewBag.USER = App_Code.CSession.Get_User(Session).U_NAME;
            ViewBag.QX = "0";
            if (App_Code.CSession.Get_User(Session).U_I01 == 1)
            { ViewBag.QX = "1";//管理员
            }

            ViewBag.Add = false;
            ViewBag.Init = true;
            ViewBag.AllInit = true;
            ViewBag.LSInit = false;
            ViewBag.AllLSInit = false;
            ViewBag.Query = true;
            ViewBag.DZBrr = false;
            ViewBag.STBrr = false;
            ViewBag.OUTBrr = false;
            ViewBag.DownLoad = false;
            ViewBag.AJZT = "0";
            ViewBag.I05 = "1";
            var columns = Models.CColumnsModel.Create_ArchiveListColumns(int.Parse(arid), true, true, true, true, true,false);

            columns = Models.CColumnsModel.Create_ArchiveListColumns(int.Parse(arid), true, false, false, false, false, false);
            ViewBag.Add = false;
            ViewBag.Init = false;
            ViewBag.AllInit = false;
            ViewBag.LSInit = false;
            ViewBag.AllLSInit = false;
            ViewBag.Query = true;
            ViewBag.DZBrr = true;
            ViewBag.STBrr = true;
            ViewBag.OUTBrr = true;
            ViewBag.DownLoad = true;
            ViewBag.AJZT = "1";
            ViewBag.I05 = "1";

            ViewBag.Columns = columns;
            ViewBag.ARID = arid;
            ViewBag.TypeN = sn;
            return View();
        }
        /// <summary>
        /// 获取档案列表
        /// </summary>
        /// <param name="where">高级查询条件</param>
        /// <param name="AJZT"></param>
        /// <param name="ARID"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get_ajaxArchiveList(string where,string AJZT, string ARID,string I05)
        {
            int count = 0;
            string sql = "";//查档的条件（是否发布，档案类型）
            string sql1 = "";//总的查档条件sql+where
            if (AJZT != "" && AJZT != null)
            {
                var listWhere = App_Code.CSession.Get_QueryWhere(Session);
                if (listWhere[0] != "" && listWhere[0] != null)
                    sql = listWhere[0]  + " and ARID= " + ARID+ " and I05="+I05;
                else
                    sql =  " and ARID= " + ARID + " and I05=" + I05;
                if (where != "" && where != null)
                    sql1 = sql + " and (" + where +")";
                else
                    sql1 = sql;
            }
            else
            {
                var listWhere = App_Code.CSession.Get_QueryWhere(Session);
                sql = listWhere[0];
                where = listWhere[1];
                if (listWhere[1] != "" && listWhere[1] != null)
                    sql1 = sql + " and (" + listWhere[1]+" ) ";
                else
                    sql1 = sql;
            }
            List<string> list1 = new List<string>(2);
            list1.Add(sql);
            list1.Add(where);
            var list = SGArchiveSystem7xDB.Linq.T_ArchiveDal.Query_Archive(sql1, out count, 0, App_Code.CWebCommons._iPageRows);
            App_Code.CSession.Add_QueryWhere(Session, list1);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取档案列表分页
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get_ajaxArchiveListPager(int num, int size)
        {
            ///分页后的起始行
            int iStart = (num - 1) * size;
            var listWhere = App_Code.CSession.Get_QueryWhere(Session);
            int count = 0;
            string sql = listWhere[0];
            if (listWhere[1] != "" && listWhere[1] != null)
                sql = sql + " and (" + listWhere[1]+" ) ";
            var list = SGArchiveSystem7xDB.Linq.T_ArchiveDal.Query_Archive(sql, out count, iStart, App_Code.CWebCommons._iPageRows);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 快速查询
        /// </summary>
        /// <param name="dh"></param>
        /// <param name="tm"></param>
        /// <param name="nd"></param>
        /// <param name="ARID"></param>
        /// <param name="I05"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get_ajaxArchiveFastQuery(string dh, string tm, string nd,string kfzt, string ARID)
        {
            int count = 0;
            string sql = " ARID=" + ARID + " and";
            if (nd != null && nd != "")
                sql += " FTM='" + nd + "' and";
            if (dh != null && dh != "")
                sql += " dh like '%" + dh + "%' and";
            if(tm != null && tm != "")
                sql += " (tm like '%" + tm + "%' or ftm like '%" + tm + "%' or gjz like '%" + tm + "%') and";
            if (kfzt != "-1")
                sql += " kfzt='" + kfzt + "'";
            else if (kfzt == "1")
                sql += " (kfzt=1 or kfzt=2)";

            if (sql.EndsWith("and")) ;
            sql = sql.TrimEnd(new char[]
            {
                    'a', 'n', 'd'
            });

            List<string> list1 = new List<string>(2);
            list1.Add(sql);
            list1.Add("");
            var list = SGArchiveSystem7xDB.Linq.T_ArchiveDal.Query_Archive(sql, out count, 0, App_Code.CWebCommons._iPageRows);
            App_Code.CSession.Add_QueryWhere(Session, list1);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 高级查询
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        [HttpPost]
        public string Get_ajaxArchiveQuery(string sql)
        {
            var listWhere = App_Code.CSession.Get_QueryWhere(Session);
            List<string> list1 = new List<string>(2);
            list1.Add(listWhere[0]);
            list1.Add(sql);
            App_Code.CSession.Add_QueryWhere(Session, list1);
            return "1";
        }
        public ActionResult NoQX()
        {
            return View();
        }
        /// <summary>
        /// 档案的添加与修改界面
        /// </summary>
        /// <param name="archivetype">档案类型id</param>
        /// <param name="id">档案id</param>
        /// <returns></returns>
        public ActionResult ArchiveAJInfo(string archivetype,string id)
        {

            var bm = SGArchiveSystem7x.App_Code.CSession.Get_Bm(Session);
            if (bm.Dep_Edit == null || bm.Dep_Edit.Length == 0)
            {
                return RedirectToAction("NoQX", "ArchiveInit"); ;
            }
            else
            {
                ViewBag.EditDepartmentID = bm.Dep_Edit.Split(',');
            }
            int count = 0;
            ViewBag.Info = SGArchiveSystem7xDB.Linq.T_ArchiveFieldsDal.Get_ArchiveFields(out count, 1, int.Parse(archivetype), -1, -1, -1, -1,1);
            ViewBag.Word = SGArchiveSystem7xDB.Linq.T_ArchiveWordDal.Get_Word(null);
            ViewBag.dept = SGArchiveSystem7x.Models.CComboboxModel.Create_ComboboxDepartment(false,false);
            ViewBag.ARID = archivetype;
            ViewBag.ID = id;
            ViewBag.AJInfo = null;
            if (id != null && id != "")
            {
                var ajinfo= SGArchiveSystem7xDB.Linq.T_ArchiveDal.Query_Archive_xls("ID = " + id);
                ViewBag.AJInfo = ajinfo;
                ViewBag.WJQX = ajinfo.Rows[0]["wjqx"];
                ViewBag.BJQX = ajinfo.Rows[0]["bjqx"];
            }           
            return View();
        }
        public ActionResult ArchiveAJQuery(string archivetype)
        {
            int count = 0;
            ViewBag.Info = SGArchiveSystem7xDB.Linq.T_ArchiveFieldsDal.Get_ArchiveFields(out count, 1, int.Parse(archivetype), -1, -1, -1, 1, -1);
            ViewBag.Word = SGArchiveSystem7xDB.Linq.T_ArchiveWordDal.Get_Word(null); ;
            ViewBag.dept = SGArchiveSystem7x.Models.CComboboxModel.Create_ComboboxDepartment(false, false);
            
            if(Models.Language._nowLanguage == SGArchiveSystem7xDB._VersionLanguage._Chs)
            {
                ViewBag.Text0 = "查询：";
                ViewBag.Text1 = "条件：";
                ViewBag.Text2 = "包含";
                ViewBag.Text3 = "等于";
                ViewBag.Text4 = "内容：";
                ViewBag.Text5 = "并且";
                ViewBag.Text6 = "或者";
                ViewBag.Text7 = "与或：";
                ViewBag.Text8 = "添加条件";
                ViewBag.Text9 = "查询条件";
                ViewBag.Text10 = "查询";
                ViewBag.Text11 = "条件";
                ViewBag.Text12 = "内容";
                ViewBag.Text13 = "与或";
                ViewBag.Text14 = "删除";
                ViewBag.Text15 = "点击查询";
                ViewBag.Text16 = "文本";
                ViewBag.Text17 = "唯一";
                ViewBag.Text18 = "数字";
                ViewBag.Text19 = "年份";
                ViewBag.Text20 = "日期";
                ViewBag.Text21 = "部门";
                ViewBag.Text22 = "提示";
                ViewBag.Text23 = "请输入查询的内容!";
                ViewBag.Text24 = "年度类型必须是4位数字!";
            }
            else
            {
                ViewBag.Text0 = "Field:";
                ViewBag.Text1 = "condition:";
                ViewBag.Text2 = "like";
                ViewBag.Text3 = "=";
                ViewBag.Text4 = "Value:";
                ViewBag.Text5 = "And";
                ViewBag.Text6 = "Or";
                ViewBag.Text7 = "And Or:";
                ViewBag.Text8 = "Add Condition";
                ViewBag.Text9 = "Search Condition";
                ViewBag.Text10 = "Field";
                ViewBag.Text11 = "Condition";
                ViewBag.Text12 = "Value";
                ViewBag.Text13 = "And Or";
                ViewBag.Text14 = "Delete";
                ViewBag.Text15 = "Start Search";
                ViewBag.Text16 = "Text";
                ViewBag.Text17 = "Key";
                ViewBag.Text18 = "Number";
                ViewBag.Text19 = "Year";
                ViewBag.Text20 = "Date";
                ViewBag.Text21 = "Dep";
                ViewBag.Text22 = "Tip";
                ViewBag.Text23 = "Please enter the content of the query";
                ViewBag.Text24 = "Year must be 4 digits";
            }

            return View();
        }
        /// <summary>
        /// 增删改档案T_ArchiveAJ
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="ARID"></param>
        /// <param name="type"></param>
        /// <param name="dh"></param>
        /// <param name="aid"></param>
        /// <returns></returns>
        public string OpArchiveAJ(string sql1,string sql2, string ARID, string type, string dh, string aid)
        {
            int value = -3;
            SGArchiveSystem7xDB.V_User user = App_Code.CSession.Get_User(Session);
            if (type == "著录")
            {
                sql1 = sql1+ "SJJLR,SJJLRQ,SJJLLX,ARID,AJZT,KFZT,I05";
                sql2 = sql2+"'"+ user.U_NAME+"','" + DateTime.Now.ToString()+ "','著录','"+ARID+ "','0','1','0'";
                string sql = "insert into T_ArchiveAj (" + sql1 + ") values (" + sql2 + ")";
                if (aid == "" && aid == null)
                    aid = "-1";
                if (SGArchiveSystem7xDB.Linq.T_ArchiveDal.Get_ISDH(-1, dh) == 0)
                    value = SGArchiveSystem7xDB.Linq.T_ArchiveDal.Op_Archive(sql, type, user, dh, -1, int.Parse(ARID));
                else
                    value = 0;
            }
            else if (type == "修改")
            {
                string sql = "update T_ArchiveAj set "+sql1.Substring(0, sql1.Length - 1) + "where ID=" + aid;
                if (SGArchiveSystem7xDB.Linq.T_ArchiveDal.Get_ISDH(int.Parse(aid), dh) == 0)
                    value = SGArchiveSystem7xDB.Linq.T_ArchiveDal.Op_Archive(sql, type, user, dh, int.Parse(aid), int.Parse(ARID));
                else
                    value = 0;
            }
            else
            {
                string sql = "DELETE FROM T_ArchiveAj WHERE DH = '"+ dh+"'";
                value = SGArchiveSystem7xDB.Linq.T_ArchiveDal.Op_Archive(sql, type, user, dh, -1, int.Parse(ARID));
            }
            return value.ToString();//>0为成功=0为重复

    }
        /// <summary>
        /// 发布
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string InitArchive(string[] dh)
        {
            //var bm = SGArchiveSystem7x.App_Code.CSession.Get_Bm(Session);
            //string EditDepartmentID = bm.Dep_Edit;                                                         
            //int Mj = SGArchiveSystem7x.App_Code.CSession.Get_User(Session).ROLE_MJ.Value;

            int value = 0;
            SGArchiveSystem7xDB.V_User user = App_Code.CSession.Get_User(Session);
            if (dh == null || dh.Length == 0)
            {
                int count = 0;
                var listWhere = App_Code.CSession.Get_QueryWhere(Session);
                string sql = listWhere[0];
                if (listWhere[1] != "" && listWhere[1] != null)
                    sql = sql + " and " + listWhere[1];
                var list = SGArchiveSystem7xDB.Linq.T_ArchiveDal.Query_Archive(sql, out count);
                value = SGArchiveSystem7xDB.Linq.T_ArchiveDal.Init(null,list, user);
            }
            else
            {
                value= SGArchiveSystem7xDB.Linq.T_ArchiveDal.Init(dh,null, user);
            }
            return value.ToString();
        }
        /// <summary>
        /// 临时发布
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string LSInitArchive(string[] dh)
        {
            //var bm = SGArchiveSystem7x.App_Code.CSession.Get_Bm(Session);
            //string EditDepartmentID = bm.Dep_Edit;                                                         
            //int Mj = SGArchiveSystem7x.App_Code.CSession.Get_User(Session).ROLE_MJ.Value;

            int value = 0;
            SGArchiveSystem7xDB.V_User user = App_Code.CSession.Get_User(Session);
            if (dh == null || dh.Length == 0)
            {
                int count = 0;
                var listWhere = App_Code.CSession.Get_QueryWhere(Session);
                string sql = listWhere[0];
                if (listWhere[1] != "" && listWhere[1] != null)
                    sql = sql + " and " + listWhere[1];
                var list = SGArchiveSystem7xDB.Linq.T_ArchiveDal.Query_Archive(sql, out count);
                value = SGArchiveSystem7xDB.Linq.T_ArchiveDal.LSInit(null, list, user);
            }
            else
            {
                value = SGArchiveSystem7xDB.Linq.T_ArchiveDal.LSInit(dh, null, user);
            }
            return value.ToString();
        }
        private string StorageRoot
        {
            get { return Path.Combine(Server.MapPath("~/Content/PDF")); }
        }
        public ActionResult Upload_PDF(string dh)
        {
            ViewBag.DH = dh;
            return View();
        }
        /// <summary>
        /// 上传
        /// </summary>
        /// <returns></returns>
        public string UploadPdf(string dh)
        {
            try
            {
                var file = Request.Files["file1"];//file1就是easyui-filebox的name
                string filename = DateTime.Now.Ticks.ToString() + file.FileName;
                var fullPath = Path.Combine(StorageRoot, Path.GetFileName(filename));
                file.SaveAs(fullPath);
                //添加压缩
                SGArchiveSystem7xCommons.SGZip.CompressFile(fullPath, Server.MapPath("~/Content/PDF/" + filename + ".sg"));
                FileInfo file1 = new FileInfo(fullPath);
                file1.Delete();
                SGArchiveSystem7xDB.V_User user = App_Code.CSession.Get_User(Session);
                SGArchiveSystem7xDB.Linq.T_ArchiveDal.Upload(dh, filename + ".sg", file.ContentLength, user);//档案id
                return "1";
            }
            catch (Exception ex)
            {
                return "0";
            }
        }
        /// <summary>
        /// 档案利用导出
        /// </summary>
        /// <param name="archivetype"></param>
        /// <returns></returns>
        public string DownLoadxlsAJ(string archivetype)
        {
            string SaveUrl = "~/Content/xls/" + DateTime.Now.Ticks.ToString() + ".xlsx";
            var listWhere = App_Code.CSession.Get_QueryWhere(Session);
            int count = 0;
            string sql = listWhere[0];
            if (listWhere[1] != "" && listWhere[1] != null)
                sql = sql + "and" + listWhere[1];
            var list = SGArchiveSystem7xDB.Linq.T_ArchiveDal.Query_Archive_xls(sql);//数据
            var listFields = SGArchiveSystem7xDB.Linq.T_ArchiveFieldsDal.Get_ArchiveFields(out count, 1, int.Parse(archivetype), -1, -1, 1, -1, -1);//字段
            var listDep = SGArchiveSystem7xDB.Linq.T_DeparetmentDal.Get_Department(out count);
            var listWord = SGArchiveSystem7xDB.Linq.T_ArchiveWordDal.Get_Word(null);
            Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
            Aspose.Cells.Cells cells = workbook.Worksheets[0].Cells;
            for (int i = 0; i < listFields.Count(); i++)
            {
                cells[0, i].PutValue(listFields[i].AF_NAME);
            }
            for (int m = 1; m <= list.Rows.Count; m++)
            {
                for (int i = 0; i < listFields.Count(); i++)
                {
                    if (listFields[i].AF_TYPE == "词典")
                    {
                        int id =int.Parse(list.Rows[m - 1][listFields[i].AF_FIELD].ToString());
                        var en = listWord.Select(c => c).Where(c => c.WD_ID== id);
                        foreach (var nn in en)
                        {
                            cells[m, i].PutValue(nn.WD_NAME);
                        }
                    }
                    else if (listFields[i].AF_TYPE == "部门")
                    {
                        int id = int.Parse(list.Rows[m - 1][listFields[i].AF_FIELD].ToString());
                        var en = listDep.Select(c => c).Where(c => c.Dep_ID == id);
                        foreach (var nn in en)
                        {
                            cells[m, i].PutValue(nn.Dep_Name);
                        }
                    }
                    else
                        cells[m, i].PutValue(list.Rows[m - 1][listFields[i].AF_FIELD].ToString());
                }
            }
            workbook.Save(Server.MapPath(SaveUrl));
            return SaveUrl.Replace("~", "..");
        }

        /// <summary>
        /// 人工出库
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public string RGCK(string[] ids)
        {
            ///获取当前操作的人
            try
            {
                SGArchiveSystem7xDB.V_User user = App_Code.CSession.Get_User(Session);
                if (user == null)
                    return "-99";

                DateTime dt = DateTime.Now;
                SGArchiveSystem7xDB.SGArchiveSystem7xEntities conn = new SGArchiveSystem7xDB.SGArchiveSystem7xEntities();

                int[] ints = new int[ids.Length];
                for (int i = 0; i < ids.Length; i++)
                    ints[i] = int.Parse(ids[i]);

                var query = conn.T_ArchiveAj.Select(c => c).Where(c => ints.Contains(c.ID));
                foreach (var n in query)
                {
                    n.KFZT = (int)SGArchiveSystem7xDB._ArchiveKFStatus._iStatus_借出;
                    n.AJZT = (int)SGArchiveSystem7xDB._ArchiveStatus._iStatus_非授权已处理;
                    ///出库时间
                    n.D01 = DateTime.Now;

                    SGArchiveSystem7xDB.T_ArchiveJK jk = new SGArchiveSystem7xDB.T_ArchiveJK();
                    jk.JK_AID = n.ID;
                    jk.JK_ARID = n.ARID;
                    jk.JK_ArchiveDH = n.DH;
                    jk.JK_ArchiveType = " ";
                    jk.JK_BINFOID = 0;
                    jk.JK_Status = 1;
                    jk.JK_Tag = n.DZWJ;
                    jk.JK_TM = n.TM;
                    jk.JK_Type = "出库(人工)";
                    jk.JK_ND = n.ND;
                    jk.JK_CLR = user.U_RNAME;
                    jk.JK_Date = dt;
                    jk.JK_FTM = n.FTM;
                    conn.T_ArchiveJK.Add(jk);
                }
                conn.SaveChanges();
                return "1";
            }
            catch { return "-1"; }
        }

        /// <summary>
        /// 人工入库
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public string RGRK(string[] ids)
        {
            ///获取当前操作的人
            try
            {
                SGArchiveSystem7xDB.V_User user = App_Code.CSession.Get_User(Session);
                if (user == null)
                    return "-99";

                DateTime dt = DateTime.Now;
                SGArchiveSystem7xDB.SGArchiveSystem7xEntities conn = new SGArchiveSystem7xDB.SGArchiveSystem7xEntities();

                int[] ints = new int[ids.Length];
                for (int i = 0; i < ids.Length; i++)
                    ints[i] = int.Parse(ids[i]);

                var query = conn.T_ArchiveAj.Select(c => c).Where(c => ints.Contains(c.ID));
                foreach (var n in query)
                {
                    n.KFZT = (int)SGArchiveSystem7xDB._ArchiveKFStatus._iStatus_在库;
                    n.AJZT = (int)SGArchiveSystem7xDB._ArchiveStatus._iStatus_非授权已处理;
                    ///入库时间
                    n.D02 = DateTime.Now;

                    SGArchiveSystem7xDB.T_ArchiveJK jk = new SGArchiveSystem7xDB.T_ArchiveJK();
                    jk.JK_AID = n.ID;
                    jk.JK_ARID = n.ARID;
                    jk.JK_ArchiveDH = n.DH;
                    jk.JK_ArchiveType = " ";
                    jk.JK_BINFOID = 0;
                    jk.JK_Status = 1;
                    jk.JK_Tag = n.DZWJ;
                    jk.JK_TM = n.TM;
                    jk.JK_Type = "入库(人工)";
                    jk.JK_ND = n.ND;
                    jk.JK_CLR = user.U_RNAME;
                    jk.JK_Date = dt;
                    jk.JK_FTM = n.FTM;
                    conn.T_ArchiveJK.Add(jk);
                }
                conn.SaveChanges();
                return "1";
            }
            catch { return "-1"; }
        }
    }
}